<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Thread</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/github.css" type="text/css" media="screen" />
<script src="../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Class</span> 
            Thread 
            
                <span class="parent">&lt; 
                    
                    <a href="Object.html">Object</a>
                    
                </span>
            
        </h1>
        <ul class="files">
            
            <li><a href="../files/activesupport/lib/active_support/core_ext/thread_rb.html">activesupport/lib/active_support/core_ext/thread.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>T</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Thread.html#method-i-thread_variable-3F">thread_variable?</a>,
              </li>
            
              
              <li>
                <a href="Thread.html#method-i-thread_variable_get">thread_variable_get</a>,
              </li>
            
              
              <li>
                <a href="Thread.html#method-i-thread_variable_set">thread_variable_set</a>,
              </li>
            
              
              <li>
                <a href="Thread.html#method-i-thread_variables">thread_variables</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    
      <!-- Section constants -->
      <div class="sectiontitle">Constants</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class="attr-name">LOCK</td>
            <td>=</td>
            <td class="attr-value">Mutex.new</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
      </table>
    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-thread_variable-3F">
            
              <b>thread_variable?</b>(key)
            
            <a href="Thread.html#method-i-thread_variable-3F" name="method-i-thread_variable-3F" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns <code>true</code> if the given string (or symbol) exists as a
thread-local variable.</p>

<pre class="ruby"><span class="ruby-identifier">me</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>
<span class="ruby-identifier">me</span>.<span class="ruby-identifier">thread_variable_set</span>(:<span class="ruby-identifier">oliver</span>, <span class="ruby-string">&quot;a&quot;</span>)
<span class="ruby-identifier">me</span>.<span class="ruby-identifier">thread_variable?</span>(:<span class="ruby-identifier">oliver</span>)    <span class="ruby-comment">#=&gt; true</span>
<span class="ruby-identifier">me</span>.<span class="ruby-identifier">thread_variable?</span>(:<span class="ruby-identifier">stanley</span>)   <span class="ruby-comment">#=&gt; false</span>
</pre>

<p>Note that these are not fiber local variables. Please see <a
href="Thread.html#method-i-thread_variable_get">#thread_variable_get</a>
for more details.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-thread_variable-3F_source')" id="l_method-i-thread_variable-3F_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/7f7c09c1f353b1775899d448a00a4eb1330a780c/activesupport/lib/active_support/core_ext/thread.rb#L61" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-thread_variable-3F_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/core_ext/thread.rb, line 61</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">thread_variable?</span>(<span class="ruby-identifier">key</span>)
  <span class="ruby-identifier">locals</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">key</span>.<span class="ruby-identifier">to_sym</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-thread_variable_get">
            
              <b>thread_variable_get</b>(key)
            
            <a href="Thread.html#method-i-thread_variable_get" name="method-i-thread_variable_get" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns the value of a thread local variable that has been set. Note that
these are different than fiber local values.</p>

<p><a href="Thread.html">Thread</a> local values are carried along with
threads, and do not respect fibers. For example:</p>

<pre class="ruby"><span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> {
  <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>.<span class="ruby-identifier">thread_variable_set</span>(<span class="ruby-string">&quot;foo&quot;</span>, <span class="ruby-string">&quot;bar&quot;</span>) <span class="ruby-comment"># set a thread local</span>
  <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>[<span class="ruby-string">&quot;foo&quot;</span>] = <span class="ruby-string">&quot;bar&quot;</span>                    <span class="ruby-comment"># set a fiber local</span>

  <span class="ruby-constant">Fiber</span>.<span class="ruby-identifier">new</span> {
    <span class="ruby-constant">Fiber</span>.<span class="ruby-identifier">yield</span> [
      <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>.<span class="ruby-identifier">thread_variable_get</span>(<span class="ruby-string">&quot;foo&quot;</span>), <span class="ruby-comment"># get the thread local</span>
      <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>[<span class="ruby-string">&quot;foo&quot;</span>],                     <span class="ruby-comment"># get the fiber local</span>
    ]
  }.<span class="ruby-identifier">resume</span>
}.<span class="ruby-identifier">join</span>.<span class="ruby-identifier">value</span> <span class="ruby-comment"># =&gt; ['bar', nil]</span>
</pre>

<p>The value <code>&quot;bar&quot;</code> is returned for the thread local,
where <code>nil</code> is returned for the fiber local. The fiber is
executed in the same thread, so the thread local values are available.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-thread_variable_get_source')" id="l_method-i-thread_variable_get_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/7f7c09c1f353b1775899d448a00a4eb1330a780c/activesupport/lib/active_support/core_ext/thread.rb#L25" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-thread_variable_get_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/core_ext/thread.rb, line 25</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">thread_variable_get</span>(<span class="ruby-identifier">key</span>)
  <span class="ruby-identifier">locals</span>[<span class="ruby-identifier">key</span>.<span class="ruby-identifier">to_sym</span>]
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-thread_variable_set">
            
              <b>thread_variable_set</b>(key, value)
            
            <a href="Thread.html#method-i-thread_variable_set" name="method-i-thread_variable_set" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Sets a thread local with <code>key</code> to <code>value</code>. Note that
these are local to threads, and not to fibers. Please see <a
href="Thread.html#method-i-thread_variable_get">#thread_variable_get</a>
for more information.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-thread_variable_set_source')" id="l_method-i-thread_variable_set_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/7f7c09c1f353b1775899d448a00a4eb1330a780c/activesupport/lib/active_support/core_ext/thread.rb#L32" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-thread_variable_set_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/core_ext/thread.rb, line 32</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">thread_variable_set</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>)
  <span class="ruby-identifier">locals</span>[<span class="ruby-identifier">key</span>.<span class="ruby-identifier">to_sym</span>] = <span class="ruby-identifier">value</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-thread_variables">
            
              <b>thread_variables</b>()
            
            <a href="Thread.html#method-i-thread_variables" name="method-i-thread_variables" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns an an array of the names of the thread-local variables (as
Symbols).</p>

<pre class="ruby"><span class="ruby-identifier">thr</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">do</span>
  <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>.<span class="ruby-identifier">thread_variable_set</span>(:<span class="ruby-identifier">cat</span>, <span class="ruby-string">'meow'</span>)
  <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>.<span class="ruby-identifier">thread_variable_set</span>(<span class="ruby-string">&quot;dog&quot;</span>, <span class="ruby-string">'woof'</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">thr</span>.<span class="ruby-identifier">join</span>               <span class="ruby-comment">#=&gt; #&lt;Thread:0x401b3f10 dead&gt;</span>
<span class="ruby-identifier">thr</span>.<span class="ruby-identifier">thread_variables</span>   <span class="ruby-comment">#=&gt; [:dog, :cat]</span>
</pre>

<p>Note that these are not fiber local variables. Please see <a
href="Thread.html#method-i-thread_variable_get">#thread_variable_get</a>
for more details.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-thread_variables_source')" id="l_method-i-thread_variables_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/7f7c09c1f353b1775899d448a00a4eb1330a780c/activesupport/lib/active_support/core_ext/thread.rb#L47" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-thread_variables_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/core_ext/thread.rb, line 47</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">thread_variables</span>
  <span class="ruby-identifier">locals</span>.<span class="ruby-identifier">keys</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    